package com.zzy.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.zzy.dao.BlobDao;
import com.zzy.model.BlobEntity;
import com.zzy.service.BlobService;

import com.zzy.utils.UtilStrToClob;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class BlobServiceImpl implements BlobService {


    @Resource
    private BlobDao bobDao;


    @Override
    public List<BlobEntity> findAll() {
        return bobDao.findAll();
    }
    public List<Map<String,Object>>   findAllMap(){
        return bobDao.findAllMap();
    }
    @Override
    public Integer save(BlobEntity model) {
        model.setTitle(System.currentTimeMillis()+"");

        //   用 JSONObject 和 blob     TypeHanderBlobJSONObject
        JSONObject blobJsonObject = new JSONObject();
        blobJsonObject.put("time",System.currentTimeMillis() );
        blobJsonObject.put("aa","asd1231");
        model.setBlobJsonObjectClumn(blobJsonObject);


        //用 JSONObject 和 String   TypeHanderJSONObject
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("aa","asd1231");
        model.setInfoJson(jsonObject);

        //用String 和 blob  TypeHanderBlobString
        model.setBlobStrClumn("这是用String读取，用blob读取" +System.currentTimeMillis() );

        // 用byte[] 和 blob TypeHanderBlobByte
        String aa = "{\"id\":\"123\",\"flag\":\"aaa-byte[]-"+System.currentTimeMillis()+"\"}";
        Blob blob = UtilStrToClob.strToBlob(aa);
        try {
            model.setBlobByteData( blob.getBytes((long)1, (int)blob.length()) );
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }



        return bobDao.save(model);
    }


}
